package com.cevent.community.app.business.controller.admin;

import com.cevent.community.app.common.dto.${Domain}Dto;
import com.cevent.community.app.common.dto.PageDto;
import com.cevent.community.app.common.dto.ResponseDataDto;
import com.cevent.community.app.common.exception.ValidatorException;
import com.cevent.community.app.common.service.${Domain}Service;
import com.cevent.community.app.common.utils.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author cevent
 * @description freemarker：${tableNameCN}接口层
 * @date 2021/9/13 18:04
 */
@RestController
@RequestMapping("/${domain}")
public class ${Domain}Controller {
private static final Logger LOG = LoggerFactory.getLogger(${Domain}Controller.class);
    //AOP日志环绕
    public static final String BUSINESS_NAME="业务模块【${tableNameCN}】";
    @Resource
    private ${Domain}Service ${domain}Service;

    //1.dto-${domain}获取
    @RequestMapping("/dtoList")
    public List<${Domain}Dto> getDtoList() {
        return ${domain}Service.${domain}DtoList();
    }


    /*2.前端分页设置，后端pageInfo处理
    前端传参方式一：表单方式--》formData 前端：直接传对象   后端参数：(Dto对象)
    前端传参方式二：流的方式--》data{param1:value1,param2:vlaue2}  后端参数：(@RequestBody Dto dto对象)
     */
    @PostMapping("/pageList")
    public ResponseDataDto list(@RequestBody PageDto pageDto) {
        LOG.info("查看controller的pageDto：{}", pageDto);
        ResponseDataDto responseData = new ResponseDataDto();
        ${domain}Service.get${Domain}PageDtoList(pageDto);
        responseData.setResponseData(pageDto);
        return responseData;
    }

    //3.新增，流方式传参，加入@RequestBody
    @PostMapping("/save")
    public ResponseDataDto save(@RequestBody ${Domain}Dto ${domain}Dto) {
        LOG.info("传入的${domain} DTO：{}", ${domain}Dto);

        <#list fieldUtilList as field>
            <#if field.nameSmallHump!="uniId"
            && field.nameSmallHump!="moduleId"
            && field.nameSmallHump!="path"
            && field.nameSmallHump!="createTime"
            && field.nameSmallHump!="updateTime"
            && field.nameSmallHump!="iCreateTime"
            && field.nameSmallHump!="iUpdateTime"
            && field.nameSmallHump!="videoUserId"
            && field.nameSmallHump!="typeId">
                <#if !field.nullAble>
        ValidatorUtil.requiredEmpty(${domain}Dto.get${field.nameBigHump}(), "${field.nameCN}");
                </#if>
                <#if (field.length>0)>
        ValidatorUtil.requiredLength(${domain}Dto.get${field.nameBigHump}(), "${field.nameCN}", 3, ${field.length?c});
                </#if>
            </#if>
        </#list>
        ResponseDataDto responseData = new ResponseDataDto();
        ${domain}Service.save(${domain}Dto);
        responseData.setResponseData(${domain}Dto);
        return responseData;
    }

    /**
     * 指定请求的格式为Delete
     * 4.删除模块，如果为多个参数，就定义多个/{param}/{param}
     */
    @DeleteMapping("/del/{uniId}")
    public ResponseDataDto del(@PathVariable String uniId) {
        LOG.info("传入的${domain} uniId：{}", uniId);
        ResponseDataDto responseData = new ResponseDataDto();
        ${domain}Service.delete(uniId);
        return responseData;
    }
}